package com.cet.algorithm.二叉树.层级遍历;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 在每个树行中找最大值
 * @author: 陈恩涛
 * @create: 2022-07-18 15:51
 **/
public class LC515 {

    public static void main(String[] args) {

    }

    public List<Integer> largestValues(TreeNode root) {
        final List<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        final Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            int max = Integer.MIN_VALUE;
            while (len > 0) {
                final TreeNode cur = queue.poll();
                max = cur.val > max ? cur.val : max;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                len --;
            }
            result.add(max);
        }
        return result;
    }
}
